﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class QuanHuyenDAL:StandardConnectDB,IQuanHuyen
    {
        public List<DTO.QuanHuyenDTO> GetListQuanHuyen()
        {
            List<QuanHuyenDTO> listQuanHuyen = new List<QuanHuyenDTO>();
            DataTable dt = this.DTExecuteSelect("Select * from QuanHuyen");
            foreach (DataRow dr in dt.Rows)
            {
                listQuanHuyen.Add(ConvertObjectDataRowToQuanHuyenDTO(dr));
            }
            return listQuanHuyen;
            
        }

        public DTO.QuanHuyenDTO GetQuanHuyenByID(string MaQuanHuyen)
        {
            QuanHuyenDTO quanhuyen = new QuanHuyenDTO();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaQuanHuyen", MaQuanHuyen);
            DataTable dt = this.DTExecuteSelect("SP_QuanHuyen_GetByID", paramlist);
            quanhuyen = ConvertObjectDataRowToQuanHuyenDTO(dt.Rows[0]);
            return quanhuyen;
            
        }

        public bool InsertQuanHuyen(DTO.QuanHuyenDTO quanhuyen)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaQuanHuyen", quanhuyen.MaQuanHuyen);
            paramlist.Add("@MaTinhThanh", quanhuyen.MaTinhThanh);
            paramlist.Add("@TenQuanHuyen", quanhuyen.TenQuanHuyen);
            paramlist.Add("@GhiChu", quanhuyen.GhiChu);

            return ExecuteUpdate("SP_QuanHuyen_Insert", paramlist);
        }

        public bool DeleteQuanHuyen(DTO.QuanHuyenDTO quanhuyen)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaQuanHuyen", quanhuyen.MaQuanHuyen);

            return this.ExecuteUpdate("SP_QuanHuyen_Delete", paramlist);
            
        }

        public bool UpdateQuanHuyen(DTO.QuanHuyenDTO quanhuyen)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaQuanHuyen", quanhuyen.MaQuanHuyen);
            paramlist.Add("@MaTinhThanh", quanhuyen.MaTinhThanh);
            paramlist.Add("@TenQuanHuyen", quanhuyen.TenQuanHuyen);
            paramlist.Add("@GhiChu", quanhuyen.GhiChu);

            return ExecuteUpdate("SP_QuanHuyen_Update", paramlist);
            
        }

        public DTO.QuanHuyenDTO ConvertObjectDataRowToQuanHuyenDTO(System.Data.DataRow dr)
        {
            if (dr != null)
            {
                QuanHuyenDTO quanhuyen = new QuanHuyenDTO();
                quanhuyen.SoTT = int.Parse(dr["SoTT"].ToString());
                quanhuyen.MaQuanHuyen = dr["MaQuanHuyen"].ToString();
                quanhuyen.TenQuanHuyen = dr["TenQuanHuyen"].ToString();
                ITinhThanh iTinhThanh = new TinhThanhDAL();
                if (dr["MaTinhThanh"].ToString() != "")
                    quanhuyen.MaTinhThanh = iTinhThanh.GetTinhThanhByID(dr["MaTinhThanh"].ToString());
                quanhuyen.GhiChu = dr["GhiChu"].ToString();

                return quanhuyen;
            }
            else
                return null;
        }


        public List<DTO.QuanHuyenDTO> GetAllQuanHuyenByMaTinhThanh(string MaTinhThanh)
        {
            List<QuanHuyenDTO> listQuanHuyen = new List<QuanHuyenDTO>();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaTinhThanh", MaTinhThanh);
            DataTable dt = this.DTExecuteSelect("SP_QuanHuyen_GetByMaTinhThanh",paramlist);
            foreach (DataRow dr in dt.Rows)
            {
                listQuanHuyen.Add(ConvertObjectDataRowToQuanHuyenDTO(dr));
            }
            return listQuanHuyen;
            
            
        }
    }
}
